package com.friendc.Controller;

import com.alibaba.druid.sql.visitor.functions.If;
import com.friendc.Service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RegistController")
public class RegistController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//设置请求域编码格式
        //设置响应编码格式
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String name=request.getParameter("name");
        UserService user = new UserService();
        if (user.findbyusername(username)){
            request.getRequestDispatcher("index.jsp").forward(request,response);
            System.out.println("用户已经存在");
        }else {
            if(user.Insert(username,password,name)){
                request.getRequestDispatcher("WEB-INF/view/login.jsp").forward(request,response);
                System.out.println("用户注册成功");
            }else{
                System.out.println("用户注册失败");
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
